<?php
	class abs_RestService
	{
		const C_STR_METHOD_CONNECT = "connect";
		const C_STR_METHOD_DELETE = "delete";
		const C_STR_METHOD_GET = "get";
		const C_STR_METHOD_HEAD = "head";
		const C_STR_METHOD_INFO = "info";
		const C_STR_METHOD_POST = "post";
		const C_STR_METHOD_PUT = "put";
		const C_STR_METHOD_TRACE = "trace";

		const C_STR_OUTPUT_FORMAT_JSON = "json";
		const C_STR_OUTPUT_FORMAT_HTML = "json";

		protected $m_str_Method = "";
		protected $m_str_OutputMethod = self::C_STR_OUTPUT_FORMAT_JSON;
		protected $m_str_DataID = "data";
		protected $m_str_TotalID = "total";

		public function __construct()
		{
			$this->m_str_Method = strtolower($_SERVER['REQUEST_METHOD']);
			$this->m_str_OutputMethod = self::C_STR_OUTPUT_FORMAT_JSON;

			switch ($this->m_str_Method)
			{
				case self::C_STR_METHOD_CONNECT:
					parse_str(file_get_contents('php://input'), $arr_Vars);
					$this->connect_Function($arr_Vars);
					break;
				case self::C_STR_METHOD_DELETE:
					parse_str(file_get_contents('php://input'), $arr_Vars);
					$this->delete_Function($arr_Vars);
					break;
				case self::C_STR_METHOD_GET:
					$this->get_Function($_GET);
					break;
				case self::C_STR_METHOD_HEAD:
					parse_str(file_get_contents('php://input'), $arr_Vars);
					$this->head_Function($arr_Vars);
					break;
				case self::C_STR_METHOD_INFO:
					parse_str(file_get_contents('php://input'), $arr_Vars);
					$this->info_Function($arr_Vars);
					break;
				case self::C_STR_METHOD_POST:
					$this->post_Function($_POST);
					break;
				case self::C_STR_METHOD_PUT:
					parse_str(file_get_contents('php://input'), $arr_Vars);
					$this->put_Function($arr_Vars);
					break;
				case self::C_STR_METHOD_TRACE:
					parse_str(file_get_contents('php://input'), $arr_Vars);
					$this->trace_Function($arr_Vars);
					break;
				default:
					$this->default_Function(null);
					break;
			}
		}

		public function setDataID($str_DataID)
		{
			$this->m_str_DataID = $str_DataID;
		}

		public function setTotalID($str_ID)
		{
			$this->m_str_TotalID = $str_ID;
		}

		public function connect_Function($arr_Values)
		{
			print("Method - " . $this->m_str_Method . "\n");
			print_r($arr_Values);
			print("<br/>\n");
		}

		public function delete_Function($arr_Values)
		{
			print("Method - " . $this->m_str_Method . "\n");
			print_r($arr_Values);
			print("<br/>\n");
		}

		public function get_Function($arr_Values)
		{
			print("Method - " . $this->m_str_Method . "\n");
			print_r($arr_Values);
			print("\n");
		}

		public function head_Function($arr_Values)
		{
			print("Method - " . $this->m_str_Method . "\n");
			print_r($arr_Values);
			print("\n");
		}

		public function info_Function($arr_Values)
		{
			$this->output_Connect_Info();
			$this->output_Delete_Info();
			$this->output_Get_Info();
			$this->output_Head_Info();
			$this->output_Info_Info();
			$this->output_Post_Info();
			$this->output_Put_Info();
			$this->output_Trace_Info();
		}

		public function output_Connect_Info()
		{
			print("<h1>CONNECT</h1>");
		}
		public function output_Delete_Info()
		{
			print("<h1>DELETE</h1>");
		}
		public function output_Get_Info()
		{
			print("<h1>GET</h1>");
			print("<p>");
			print("The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI. If the Request-URI " .
				"refers to a data-producing process, it is the produced data which shall be returned as the entity in the response and not the source " .
				"text of the process, unless that text happens to be the output of the process.");
			print("</p><p>");
			print("The semantics of the GET method change to a \"conditional GET\" if the request message includes an If-Modified-Since, If-Unmodified-Since, " .
				"If-Match, If-None-Match, or If-Range header field. A conditional GET method requests that the entity be transferred only under the " .
				"circumstances described by the conditional header field(s). The conditional GET method is intended to reduce unnecessary network usage " .
				"by allowing cached entities to be refreshed without requiring multiple requests or transferring data already held by the client.");
			print("</p><p>");
			print("The semantics of the GET method change to a \"partial GET\" if the request message includes a Range header field. A partial GET requests " .
				"that only part of the entity be transferred, as described in section 14.35. The partial GET method is intended to reduce unnecessary " .
				"network usage by allowing partially-retrieved entities to be completed without transferring data already held by the client.");
			print("</p><p>");
			print("The response to a GET request is cacheable if and only if it meets the requirements for HTTP caching described in section 13.");
			print("</p>");
		}
		public function output_Head_Info()
		{
			print("<h1>HEAD</h1>");
		}
		public function output_Info_Info()
		{
			print("<h1>INFO</h1>");
		}
		public function output_Post_Info()
		{
			print("<h1>POST</h1>");
		}
		public function output_Put_Info()
		{
			print("<h1>PUT</h1>");
		}
		public function output_Trace_Info()
		{
			print("<h1>TRACE</h1>");
		}

		public function post_Function($arr_Values)
		{
			print("Method - " . $this->m_str_Method . "\n");
			print_r($arr_Values);
			print("\n");
		}

		public function put_Function($arr_Values)
		{
			print("Method - " . $this->m_str_Method . "\n");
			print_r($arr_Values);
			print("\n");
		}

		public function trace_Function($arr_Values)
		{
			print("Method - " . $this->m_str_Method . "\n");
			print_r($arr_Values);
			print("\n");
		}

		public function default_Function($arr_Values)
		{
			print("Method - default<br/>\n");
			print_r($arr_Values);
			print("\n");
		}

		public function output_Error($str_Message)
		{
			$arr_Output = array();

			$arr_Output["success"] = false;
			$arr_Output["errors"] = array();
			$arr_Output["errors"]["msg"] = $str_Message;

			$this->output_Array($arr_Output);
		}

		public function output_Data($arr_Data, $int_Size = false, $arr_AddData = null)
		{
			$arr_Output = array();

			if (!$int_Size)
			{
				$int_Size = sizeof($arr_Data);
			}

			if ($arr_AddData)
			{
				$arr_Output = $arr_AddData;
			}

			$arr_Output["success"] = true;
			$arr_Output[$this->m_str_DataID] = $arr_Data;
			$arr_Output[$this->m_str_TotalID] = $int_Size;

			$this->output_Array($arr_Output);
		}

		public function set_OutputMethod($str_Method)
		{
			$this->m_str_OutputMethod = $str_Method;
		}

		public function output_Array($arr_Array)
		{
			switch ($this->m_str_OutputMethod)
			{
				case self::C_STR_OUTPUT_FORMAT_JSON:
					$this->output_JSON($arr_Array);
					break;
				case self::C_STR_OUTPUT_FORMAT_HTML:
					print("Write HTML output Method");
					break;
				default:
					print_r($arr_Array);
			}
		}

		public function output_JSON($arr_Array)
		{
			header('Content-type: application/json; charset=UTF-8');
			die(json_encode($arr_Array));
		}

		public function output_Direct($str_Data)
		{
			die($str_Data);
		}

	}
?>